<?php

class IndexController extends Controller_Action
{
	public $_sourceDefault = 'pikabu';
	
	public function indexAction()
	{
		
	}

	public function pikabuAction()
	{
		Page::getInstance()->setSourceKey('pikabu');
		$this->view->post = PostPeer::getInstance()->getPost();
	}

	public function kotoAction()
	{
		Page::getInstance()->setSourceKey('koto');
		$this->view->post = PostPeer::getInstance()->getPost();
	}

	public function parserCommunityAction()
	{
		if ($this->getRequest()->isXmlHttpRequest()) {
			if (! $this->_getParam('group_url')) {
				throw new Zend_Controller_Action_Exception(
						'Не указана ссылка на группу');
			}
		}
	}

	public function logAction()
	{
		$this->_ajaxOnly();
		$this->_setAjaxResponse($this->_log->getLog());
	}

	public function groupAction()
	{
		// ID авторизированного пользователя
		$user_id = $this->_vk->getUser();
		if (! $user_id) {
			$this->_redirect('/index/login');
		}
		$params = $this->_getActionParams();
		$this->view->sources = SourcePeer::getInstance()->fetchAll()->toArray();
		$this->view->source_pikabu = array(
				'best_count' => PostPeer::getInstance()->countPikabu('best'),
				'untreated_count' => PostPeer::getInstance()->countPikabu(
						'untreated')
		);
		$this->view->source_koto = array(
				'untreated_count' => PostPeer::getInstance()->countKoto()
		);
		$_info = UserPeer::getInstance()->getUserInfo($user_id);
		$this->view->groups = Zend_Json::decode($_info['groups'], true);
		$this->view->name = $_info['name'];
		$this->view->activity = ActivityPeer::getInstance()->getList(20);
		if (isset($params['id'])) {
			$groups = Zend_Json::decode($_info['groups'], true);
			$w_path = '/images/groups/' . $params['id'] . '/watermark.png';
			if (! is_file($_SERVER['DOCUMENT_ROOT'] . $w_path)) {
				$w_path = 'none';
			}
			foreach ($groups as $group) {
				if ($group['gid'] == $params['id']) {
					$this->view->group = $group;
					$this->view->w_img = $w_path;
					$this->view->members = $this->_vk->api('groups.getMembers', 
							array(
									'gid' => $group['gid']
							));
					unset($this->view->members['users']);
					$this->_log->info(
							'Edit group: vk api request (groups.getMembers) params= ' .
									 $this->_log->array_render(
											array(
													'gid' => $group['gid']
											)) . '; response=' .
									 $this->_log->array_render(
											$this->view->members));
					break;
				}
			}
		} else {
			$this->_log->err('Edit group: key id is not set');
		}
	}

	public function wuploadAction()
	{
		$this->_disableView();
		$params = $this->_getActionParams();
		$this->_log->info(
				'Edit group: upload watermark ' .
						 $this->_log->array_render($_FILES));
		if (is_uploaded_file($_FILES["filename"]["tmp_name"])) {
			move_uploaded_file($_FILES["filename"]["tmp_name"], 
					$_SERVER['DOCUMENT_ROOT'] . '/images/groups/' . $params['id'] .
							 '/watermark.png');
			$this->_log->info(
					'Edit group: upload watermark final ' .
							 $_SERVER['DOCUMENT_ROOT'] . '/images/groups/' .
							 $params['id'] . '/watermark.png');
		}
		$this->_redirect('/index/group?id=' . $params['id']);
	}
}
